package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: ShoppingCartController    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2022/12/22 15:13   // 时间
 * @Version: 1.0     // 版本
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 将菜品或者套餐添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){

        //输出日志
        log.info("购物车数据：{}",shoppingCart);

        //给用户id属性进行
        Object user = session.getAttribute("user");
        Long userId = (Long) user;
        shoppingCart.setUserId(userId);

        //查询当前用户的菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if (dishId != null){
            //说明添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //说明添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        List<ShoppingCart> one = shoppingCartService.list(queryWrapper);


        if (one.size() != 0){

            Integer size = one.size();
            //如果已经存在，数量的基础上+1,则执行新增操作
            shoppingCart.setNumber(size + 1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
            //如果不存在，就添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //one = shoppingCart;
        }

        return R.success(shoppingCart);
    }


    /**
     * 点击`-`号，自动去除最近新添加的菜品
     * @param shoppingCart //必须这样封装，因为前端传来的可能是dish_id，也可能是setmeal_id
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session) {

        //判断当前传回来的参数是dish_id还是setmeal_id
        Long dishId = shoppingCart.getDishId();
        //查询当前登录对象的user_id
        Object user = session.getAttribute("user");
        Long userId = (Long) user;
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId != null){
            //说明前端传回来的是dish_id
            //根据dish_id查询shoppingCart对象
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
            //点击`-`号，自动去除最近新添加的菜品

            //重新构造条件构造器
            LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ShoppingCart::getUserId,userId);
            queryWrapper1.eq(ShoppingCart::getDishId,dishId);
            queryWrapper1.eq(ShoppingCart::getNumber,list.size());
            shoppingCartService.remove(queryWrapper1);

        } else {
            //说明前端传回来的是setmeal_id
            //根据setmeal_id查询shoppingCart对象
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
            //点击`-`号，自动去除最近新添加的菜品

            //重新构造条件构造器
            LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ShoppingCart::getUserId,userId);
            queryWrapper1.eq(ShoppingCart::getDishId,dishId);
            queryWrapper1.eq(ShoppingCart::getNumber,list.size());
            shoppingCartService.remove(queryWrapper1);

        }
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车中的菜品和套餐
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){

        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 清空购物车
     * @param session
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clear(HttpSession session){

        log.info("清空购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        shoppingCartService.remove(queryWrapper);
        return R.success("已清空购物车");
    }
}
